package com.zhuidian.mothernote.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;

import androidx.viewpager.widget.ViewPager;

import com.base.adapter.WrapperFragmentPagerAdapter;
import com.base.presenter.BasePresenter;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;
import com.zhuidian.mothernote.R;
import com.zhuidian.mothernote.activity.QuestionActivitiy;
import com.zhuidian.mothernote.activity.SearchActivity;
import com.zhuidian.mothernote.global.BaseFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class HomeFragment extends BaseFragment {

    private AppBarLayout appbarLayout;
    private LinearLayout ll_question;
    private LinearLayout ll_search;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private FollowFragment followFragment;
    private IntroduceFragment introduceFragment;
    private SubscribeFragment subscribeFragment;


    @Override
    protected int inflateMainLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(View view) {
        appbarLayout = (AppBarLayout) view.findViewById(R.id.appbarLayout);
        ll_search = (LinearLayout) view.findViewById(R.id.ll_search);
        ll_question = (LinearLayout) view.findViewById(R.id.ll_question);
        tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        List titleList = Arrays.asList(getResources().getStringArray(R.array.home_classification));
        tabLayout.addTab(tabLayout.newTab().setText((String) titleList.get(0)));
        tabLayout.addTab(tabLayout.newTab().setText((String) titleList.get(1)));
        tabLayout.addTab(tabLayout.newTab().setText((String) titleList.get(2)));
        ArrayList<Fragment> fragmentList = new ArrayList<>();
        followFragment = new FollowFragment();//关注
        introduceFragment = new IntroduceFragment();//推荐
        subscribeFragment = new SubscribeFragment();
        fragmentList.add(followFragment);
        fragmentList.add(introduceFragment);
        fragmentList.add(subscribeFragment);
        WrapperFragmentPagerAdapter wrapperFragmentPagerAdapter = new WrapperFragmentPagerAdapter(getFragmentManager(), fragmentList);
        wrapperFragmentPagerAdapter.setPageTitleList(titleList);
        viewPager.setAdapter(wrapperFragmentPagerAdapter);
        viewPager.setOffscreenPageLimit(fragmentList.size());
        tabLayout.setupWithViewPager(viewPager);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initValue() {

    }

    @Override
    protected void setListener() {
        ll_search.setOnClickListener(this);
        ll_question.setOnClickListener(this);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_search:
                context.startActivity(new Intent(context, SearchActivity.class));
                break;
            case R.id.ll_question:
                context.startActivity(new Intent(context, QuestionActivitiy.class));
                break;
        }
    }

}
